<?php

class OfferDAO extends DAO {

    public static function getoffer($uid = 0, $tuid = 0)
	{
		$uid = intval($uid);
		$tuid = intval($tuid);
        if (!$uid || !$tuid) return false;
        
        $dbconf = self::dbhash($uid, "pvn_global", "offer");
        $dbconn = self::getconn($uid, $dbconf, __FUNCTION__);
        if (!$dbconn) return false;
        
        $sql = "select * from {$dbconf[4]} where uid={$uid} and tuid={$tuid}";
        $ret = self::dbquery($dbconn, $sql, "row");
        self::dbclose($dbconn);
        if ($ret === false) return false;
        
        return $ret;
	}

	public static function getoffersbypost($uid = 0)
	{
		$uid = intval($uid);
        if (!$uid) return false;
        
        $dbconf = self::dbhash($uid, "pvn_global", "offer");
        $dbconn = self::getconn($uid, $dbconf, __FUNCTION__);
        if (!$dbconn) return false;
        
        $sql = "select * from {$dbconf[4]} where uid={$uid}";
        $ret = self::dbquery($dbconn, $sql, "rows");
        self::dbclose($dbconn);
        if ($ret === false) return false;
        
        return $ret;
	}

	public static function getoffersbytarget($uid = 0)
	{
		$uid = intval($uid);
        if (!$uid) return false;
        
        $dbconf = self::dbhash($uid, "pvn_global", "offer");
        $dbconn = self::getconn($uid, $dbconf, __FUNCTION__);
        if (!$dbconn) return false;
        
        $sql = "select * from {$dbconf[4]} where tuid={$uid}";
        $ret = self::dbquery($dbconn, $sql, "rows");
        self::dbclose($dbconn);
        if ($ret === false) return false;
        
        return $ret;
	}

	public static function getnunpostoffers()
	{
        $dbconf = self::dbhash($uid, "pvn_global", "offer");
        $dbconn = self::getconn($uid, $dbconf, __FUNCTION__);
        if (!$dbconn) return false;
        
        $sql = "select count(*) from {$dbconf[4]} where ptm=0";
        $ret = self::dbquery($dbconn, $sql, "value");
        self::dbclose($dbconn);
        if ($ret === false) return false;
        
        return $ret;
	}

	public static function getunpostoffers($page = 1, $pagesize = 50)
	{
		$page = intval($page);
		$page = $page ? $page : 1;
		$pagesize = intval($pagesize);
		$pagesize = $pagesize ? $pagesize : 50;
		$start = ($page - 1) * $pagesize;
        
        $dbconf = self::dbhash($uid, "pvn_global", "offer");
        $dbconn = self::getconn($uid, $dbconf, __FUNCTION__);
        if (!$dbconn) return false;
        
        $sql = "select uid, tuid from {$dbconf[4]} where ptm=0 limt {$start} {$pagesize}";
        $ret = self::dbquery($dbconn, $sql, "rows");
        self::dbclose($dbconn);
        if ($ret === false) return false;
        
        return $ret;
	}

	public static function setoffer($uid = 0, $tuid = 0, $data = array())
	{
		$uid = intval($uid);
		$tuid = intval($tuid);
		if (!$uid || !$tuid || !$data || !is_array($data)) return false;
    
        $sets = array();
		foreach($data as $k => $v)
		{
			switch($k)
			{
				case 'price':
				case 'gb':
				case 'ctm':
				case 'ptm':
				case 'pcount':
				case 'auid':
				case 'actm':
				case 'acount':
				case 'aperson':
					$v = intval($v);
					$sets[] = "{$k}='{$v}'";
					break;
				case 'apersonlist':
					$v = mysql_escape_string($v);
					$sets[] = "{$k}='{$v}'";
					break;
			}
		}
		if(!$sets) return false;
		$sets = implode(",", $sets);
        
		$dbconf = self::dbhash($uid, "pvn_global", "offer");
		$dbconn = self::getconn($uid, $dbconf, __FUNCTION__);
		if (!$dbconn) return false;
		
		$sql = "update {$dbconf[4]} set {$sets} where uid={$uid} and tuid={$tuid}";
		$ret = self::dbquery($dbconn, $sql);
		self::dbclose($dbconn);
		
		return $ret;
	}

	public static function newoffer($uid = 0, $tuid = 0, $data = array())
	{
		$uid = intval($uid);
		$tuid = intval($tuid);
		if (!$uid || !$tuid || !$data || !is_array($data)) return false;
    
        $sets = array();
		foreach($data as $k => $v)
		{
			switch($k)
			{
				case 'price':
				case 'gb':
				case 'ctm':
				case 'ptm':
				case 'pcount':
				case 'auid':
				case 'actm':
				case 'acount':
				case 'aperson':
					$v = intval($v);
					$sets[] = "{$k}='{$v}'";
					break;
				case 'apersonlist':
					$v = mysql_escape_string($v);
					$sets[] = "{$k}='{$v}'";
					break;
			}
		}
		if(!$sets) return false;
		$sets = implode(",", $sets);
        
		$dbconf = self::dbhash($uid, "pvn_global", "offer");
		$dbconn = self::getconn($uid, $dbconf, __FUNCTION__);
		if (!$dbconn) return false;

        $ret = self::dbquery($dbconn, "insert into {$dbconf[4]} set {$sets}");
        self::dbclose($dbconn);
        
        return $ret;
	}

	public static function deloffer($uid = 0, $tuid = 0)
	{
		$uid = intval($uid);
		$tuid = intval($tuid);
		if (!$uid || !$tuid) return false;
    
		$dbconf = self::dbhash($uid, "pvn_global", "offer");
		$dbconn = self::getconn($uid, $dbconf, __FUNCTION__);
		if (!$dbconn) return false;
		
		$sql = "delete from {$dbconf[4]} where uid={$uid} and tuid={$tuid}";
		$ret = self::dbquery($dbconn, $sql);
		self::dbclose($dbconn);
		
		return $ret;
	}
}

?>